package com.algorithm.impl.string;

/**
 * @author: ZhouBert
 * @date: 2019/9/27
 * @description:
 */
public class BruteForceBF {

	public static int bruteForce(String source, String input){
		///-1代表不存在
		if (source==null||input==null ||source.length()<input.length()){
			return -1;
		}
		///0代表存在
		if (input.length()==0){
			return 0;
		}
		char[] sChars = source.toCharArray();
		char[] iChars = input.toCharArray();
		for (int i = 0; i < sChars.length-iChars.length+1; i++) {
			///1.如果第一个字符匹配上了，进行模式串所有匹配
			if (sChars[i]==iChars[0]){
				boolean isContains=true;
				for (int j = 0; j < iChars.length; j++) {
					if (sChars[i]!=iChars[j]){
						isContains=false;
						break;
					}
				}
				if (isContains){
					return i;
				}
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		String haystack = "hello", needle = "ll";
		///haystack = "a"; needle = "a";
		System.out.println(bruteForce(haystack, needle));
	}
}
